package com.eascs.ticket.configuration.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.configuration.entity.DictionarySeq;

@Service
public class DictionarySeqService extends BaseSevice {

	public static final int INIT_SEQ = 10001;

	@Transactional(transactionManager = "ticketTransactionManager")
	public String buildSeq(String code, String prefix) {
		int seq = INIT_SEQ;
		DictionarySeq dictionarySeq = new DictionarySeq();
		dictionarySeq.setCode(code);
		dictionarySeq.setPrefix(prefix);
		DictionarySeq dictionarySeqDb = this.proxyDao.findEntityWithAny(dictionarySeq);
		if (null == dictionarySeqDb) {
			dictionarySeq.setCurrent(seq);
			EntityUtil.initInsertEntity(dictionarySeq);
			this.proxyDao.insert(dictionarySeq);
		} else {
			dictionarySeq = dictionarySeqDb;
			seq = dictionarySeq.getCurrent() + 1;
			dictionarySeq.setCurrent(seq);
			EntityUtil.initUpdateEntity(dictionarySeq);
			this.proxyDao.updateById(dictionarySeq);
		}
		return prefix + seq;
	}

	public List<Object[]> getAllDictionaryType() {
		return this.proxyDao.findObjectsList("select id,code,prefix,start_with,current,init_length,comments from dictionary_seq order by code");
	}

}
